<html xmlns:th="http://www.thymeleaf.org">
<head th:replace="admin/public/form::head(_)"/>
<body th:replace="admin/public/form::body(~{::div})">
<div class="layui-card">
    <div class="layui-card-header ">
        <span class="layui-breadcrumb">
          <a href="/admin" target="_blank">首页</a>
          <a href="javascript:;">导航栏管理</a>
          <a href="javascript:;" onclick="parent.xadmin.add_tab('导航栏列表','/admin/nav/index')">导航栏列表</a>
          <a><cite>添加导航栏</cite></a>
        </span>
        <a class="layui-btn layui-btn-sm" style="line-height:1.6em;margin-top:3px;float:right"
           href="javascript:location.replace(location.href);" title="刷新">
            <i class="layui-icon layui-icon-refresh-3" style="line-height:30px"></i></a>
    </div>
    <div class="layui-card-body ">
        <form id="form1" class="layui-form">
            <div class="layui-form-item">
                <label class="layui-form-label">父级导航栏</label>
                <div class="layui-input-inline" style="width: auto">
                    <select name="pid" id="pid-select" lay-verify="required" lay-filter="pid-select" lay-search>
                        <option value="0" data-level="1">顶级导航栏</option>
                        <option th:each="nav:${navs}" th:value="${nav.id}" th:text="${nav.title}">顶级资讯分类</option>
                    </select>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">导航栏名称</label>
                <div class="layui-input-block">
                    <input type="text" name="title" lay-verify="required" lay-reqtext="请输入导航栏名称" placeholder="请输入导航栏名称"
                           autocomplete="off" class="layui-input ">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">跳转链接</label>
                <div class="layui-input-block">
                    <input type="text" name="url" lay-reqtext="请输入跳转链接" placeholder="跳转链接"
                           autocomplete="off" class="layui-input ">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">排序</label>
                <div class="layui-input-inline">
                    <input type="text" name="sorts" lay-verify="number|required" value="100" lay-reqtext="排序必须为数字"
                           placeholder="导航栏排序" autocomplete="off" class="layui-input ">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">状态</label>
                <div class="layui-input-inline">
                    <input type="radio" name="status" title="显示" value="1" checked/>
                    <input type="radio" name="status" title="隐藏" value="0"/>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit="" lay-filter="add">立即提交</button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </div>
            <input type="hidden" name="level" value="0"/>
        </form>
    </div>
</div>
</body>
<script>
    layui.use(['jquery', 'form', 'layer','element'], function () {
        var $ = layui.jquery,
            form = layui.form,
            layer = layui.layer;
        layui.element.render();
        //监听提交
        form.on('submit(add)', function (data) {
            let send_json = data.field;
            send_json.level = $("#pid-select option:selected").data('level');
            //发异步，把数据提交给php
            $.ajax({
                url: '/admin/nav/add',
                method: 'post',
                data: send_json,
                dataType: 'JSON',
                success: function (res) {
                    if (res.code == 0) {
                        layer.alert("添加成功", {
                            icon: 1
                        }, function () {
                            // 获得frame索引
                            window.location.reload();
                        });
                    } else {
                        layer.msg(res.msg, {icon: 5});
                    }
                },
                error: function (data) {
                    layer.msg('服务器繁忙', {icon: 5});
                }
            });

            return false;
        });
    });
</script>
</html>